Lesson 3 - Variables Input and Selection

Do It Now

Create a OneNote Page and copy this into the title:

Lesson 3 - Spring 2 - Variables, Inputs and Selection



Complete this and screenshot it into OneNote - then go to the next slide in this section

⌨️ Setting Up For Today

Click this link: VSCODEEDU.COM


  • Sign In with your Microsoft Account
  • Create a New project and call it Y8 - SP2.3 - Variables and Selection
  • Rename main.py as Activity 1.py
  • Share your project and copy the link into OneNote

🎯 Learning Objectives


  • LO1:We will revisit and consolidate our understanding of Inputs, Variables and Outputs
  • LO2:We will learn how to build decision making programs with selection

🐍 Practical Coding Activity

You will be editing Activity 1.py
Paste this comment onto the first line:

# 🐍 - Activity 1 - Consolidation of Inputs and Variables

  • Using Inputs, Variables and the print() function...
  • Create a python program that outputs this conversation:
Enter your name: Janet
Hello Janet, nice to meet you
Enter the destination you are flying to: Madrid
Enter the time you are flying: 16:35
Perfect, one flight for Janet, flying to Madrid at 16:35 
Enter the seat type you would like (economy or business): economy
Perfect, you have been booked in for economy
You will receive your seat number via email
Click for help

(1)To assign a user input to a variable

fruit = input("Enter the name of your favourite fruit: ")

(2)To output a variable in an fString

print(f"Wow {fruit} is always considered a healthy option!")


Extension Task
  • Create a new file called Activity 1 - EXT.py
  • Write the script for a new conversation (about any topic of your choice!)

🧠 Reflection Task

📝 MWB Reflection Task

Open the mini-whiteboard app in a new tab

Your teacher will start with these leading questions:

  • What function allows the user to enter text into our program?
  • What do we call the instructions we give to the user?
  • If we output a string of text where we have variables in curly brackets. What must we place at the start of the string?

🐍 Working With Selection

  • Selection involves making decisions
  • To work effectively with selection we can use the IF and ELSE commands
  • # Using selection to test and answer
    question = input("Enter the capital of France: ")
    
    if question.lower() == "paris":
        print("Correct")
    else:
        print("Incorrect")
        
    

    The code above has some important points to consider.
    • The end of each test (IF or ELSE) must be completed with a colon :
    • Notice that when we compare one value to another we use a double equals sign :
    • The code that happens as a result of this test must be indented (four spaces from the left). This will happen automatically if you have ended the test with a colon)
      • Your teacher will show you how to use the tab key to indent code
    • Python is case-sensitive so we put .lower() at the end of the variable. This will convert to all lowercase, so we can then test if it is equal to our expected answer (all in lowercase).

🐍 Coding Activity 2 (Part 1)

Create a new file in VSCODE called Activity 2.py

Copy and paste this comment into the first line
# 🐍 Activity 2 - Selection with IF and ELSE


Using a variable and an input() function

  • Create a program that asks the user a question and tests to see if they have answered correctly
  • If they have answered correctly, output "Correct"
  • If they have answered incorrectly, output "Incorrect"


The program should output something like this

Enter the capital city of Norway: Oslo
Correct!

🐍 Coding Activity 2 (Part 2)

  • Now you must create more questions and turn your program into a full quiz
  • Make sure each question has its own variable!


What if there is more than one correct answer?

If a question has more than one correct answer you can use ELIF

colour = input("Name a primary colour: ")
if colour.lower() == "red":
    print("Correct")
elif colour.lower() == "blue":
    print("Correct")
elif colour.lower() == "yellow":
    print("Correct")
else:
    print("Incorrect")

Plenary Activity

📝 MWB Reflection Task

Open the mini-whiteboard app in a new tab

Your teacher will start with these leading questions:

  • What must be put at the end of an IF / ELSE test?
  • Why do we use .lower()